import e from "./scroll_view";
import t from "./rect";
import n from "./vec2";

const r = {
  isVertical(e) {
    return !1;
  },
  cellCount(e) {
    return 0;
  },
  cellSizeByIndex(e, t) {
    return 0;
  },
  cellByIndex(e, t) {
    return null;
  },
};

export default class TableView extends e {
  constructor() {
    super();
    // TODO: i.constructor = i
  }
  _dataSource = null;
  dataSource(e?) {
    let t = this;
    return (
      Boolean(e) &&
        e != t._dataSource &&
        ((t._dataSource = e),
        e.isVertical(t)
          ? (t.lockHorizontal(!0), t.lockVertical(!1))
          : (t.lockVertical(!0), t.lockHorizontal(!1))),
      t._dataSource
    );
  }
  renderView() {
    let e = this;
    if (e._dataSource != null) {
      if (e.bouncing() == 0) {
        let t = e.offset();
        (t.x = Math.max(0, Math.min(e.contentSize().x - e.width(), t.x))),
          (t.y = Math.max(0, Math.min(e.contentSize().y - e.height(), t.y)));
      }
      let n = e.contentView();
      n.width(e.width()), n.height(e.height());
      let r;
      e._dataSource.isVertical(e)
        ? (r = e.renderVertical())
        : (r = e.renderHorizontal());
      let i = e.contentView().subViews().slice();
      for (let s = 0; s < i.length; s++) {
        let o = !0;
        for (let u = 0; u < r.length; u++)
          if (i[s] == r[u]) {
            o = !1;
            break;
          }
        o && i[s].remove();
      }
      for (let a = 0; a < r.length; a++) {
        let f = !0;
        for (let l = 0; l < i.length; l++)
          if (r[a] == i[l]) {
            f = !1;
            break;
          }
        f && e.contentView().addSubView(r[a]);
      }
      e._hScrollBar != null &&
        (e._hScrollBar.updateSize(), e._hScrollBar.updatePotion()),
        e._vScrollBar != null &&
          (e._vScrollBar.updateSize(), e._vScrollBar.updatePotion());
    }
  }
  renderVertical() {
    let e = this,
      t = new n(e.offset().y, e.offset().y + e.height()),
      r = [],
      i = 0;
    for (let s = 0; s < e.dataSource().cellCount(e); s++) {
      let o = e.dataSource().cellSizeByIndex(e, s),
        u = new n(i, i + o),
        a = u.y < t.x || u.x > t.y;
      if (a == 0) {
        let f = e.dataSource().cellByIndex(e, s);
        f.x(0), f.y(i - e.offset().y), r.push(f);
      }
      i += o;
    }
    return (e._contentSize = new n(0, i)), r;
  }
  renderHorizontal() {
    let e = this,
      t = new n(e.offset().x, e.offset().x + e.width()),
      r = [],
      i = 0;
    for (let s = 0; s < e.dataSource().cellCount(e); s++) {
      let o = e.dataSource().cellSizeByIndex(e, s),
        u = new n(i, i + o),
        a = u.y < t.x || u.x > t.y;
      if (a == 0) {
        let f = e.dataSource().cellByIndex(e, s);
        f.x(i - e.offset().x), f.y(0), r.push(f);
      }
      i += o;
    }
    return (e._contentSize = new n(i, 0)), r;
  }
}
